package com.xiaoyi.ui.fragment.pic;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.avin.yi.R;


import com.xiaoyi.entity.XyData;
import com.xiaoyi.entity.XyDataQueryResult;
import com.xiaoyi.listener.OnMusicItemClickListener;
import com.xiaoyi.tool.AvBaseTool;
import com.xiaoyi.tool.http.XyApi;
import com.xiaoyi.picshow.BigPhotoPagerAdapter;
import com.xiaoyi.picshow.MyViewPager;
import com.xiaoyi.ui.fragment.base.BaseSwipeBackFragment;
import com.xiaoyi.ui.view.TxtItemListDialog;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.SingleObserver;
import io.reactivex.disposables.Disposable;


public class PicDetailFragment extends BaseSwipeBackFragment {
    private static final String ARG_ITEM = "arg_item";

    private FloatingActionButton mFab;
    private String type ;

    MyViewPager mViewPager ;
    BigPhotoPagerAdapter mAdapter ;
    private TextView showprogress;

    public static PicDetailFragment newInstance(String type) {

        Bundle args = new Bundle();
        args.putString(ARG_ITEM , type);
        PicDetailFragment fragment = new PicDetailFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type = getArguments().getString(ARG_ITEM);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.picshow_fragment, container, false);
        initView(view);
        return attachToSwipeBack(view);
        /// 不能右滑返回
        //return view;
    }



    private void initView(View view) {
        mFab =  view.findViewById(R.id.fab);
        mFab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop();
            }
        });
        mViewPager = view.findViewById(R.id.bigPotoViewPager);
        showprogress = view.findViewById(R.id.showprogress) ;
        view. findViewById(R.id.showDialog).setOnClickListener(onClickListener);
        loadData( );
    }

    private void loadData(    ) {
        showLoadDialog2(_mActivity);
        XyApi.getInstance(_mActivity).getDataByTypeAndDir( new SingleObserver<XyDataQueryResult>() {
            @Override
            public void onSubscribe(Disposable d) {    }

            @Override
            public void onSuccess(XyDataQueryResult  result) {
                updateData(result.getList() );
                hideLoadDialog2();

            }

            @Override
            public void onError(Throwable e) {
                AvBaseTool.showMsg(_mActivity ,"错误："+ e.getMessage());
                hideLoadDialog2();
            }
        } , "图片" ,type  ,1000 , 1 ,false );

    }

    private  List<XyData> list ;
    private int currentPostion ;
    private void updateData(final  List<XyData>  result) {
        list = result ;
        mAdapter = new BigPhotoPagerAdapter(_mActivity  ,list ) ;
        mViewPager.setAdapter( mAdapter);
        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setCurrentItem(0);

        mViewPager.setOnPageChangeListener(new MyViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                showprogress.setText( (position+1)+"/"+list.size());
                currentPostion = position ;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.showDialog:
                    showList();
                    break;
                default:
                    break;
            }

        }
    };


    private TxtItemListDialog musicListDialog ;
    public void showList( )
    {
        if(musicListDialog == null)
        {
            musicListDialog = new TxtItemListDialog(  _mActivity ,getStringList (list) );
            musicListDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    musicListDialog = null;
                }
            });

            musicListDialog.setOnItemClickListener(new OnMusicItemClickListener() {
                @Override
                public void onItemClick(int  postion  ) {
                    mViewPager.setCurrentItem(postion);

                }
            });
        }else{
            musicListDialog.setDatas(getStringList (list));
        }
        musicListDialog.setSelectedItemColor(currentPostion);
        musicListDialog.show();
    }


    private List<String> getStringList( List<XyData> list){
        List<String> reslut =new ArrayList<>() ;
        for(XyData bean : list){
            reslut.add(bean.getmName()) ;
        }
        return  reslut ;
    }
}
